package com.szxx.mahjong.qianjiang.domain;

import java.util.ArrayList;
import java.util.List;


public class TingCheckResult 
{
	//里面不是aaa,就是abc这样的牌
	public List<Integer> checked=new ArrayList<Integer>();
	//里面是还没有检测的牌
	public List<Byte> unchecked=new ArrayList<Byte>();

	//组成牌后剩余的几张牌
	//public List<Byte> lastCards = new  ArrayList<Byte>();
	
	public int danDiao=0;
	public int duiZi=0;
	public int shunZi=0;//听顺子
	public int result=0;
	public int keziNum=0;//刻牌数量，胡牌的时候需要判定，听牌不需要判断
	public int shunZiNum=0;//成牌的，顺子数量
	
	public int jiang2Num = 0;
	public int jiang5Num = 0;
	public int jiang8Num = 0;
	public boolean has258jiang = false;
	
	//是否听夹
	//public boolean hastingjia = false;
	
	//听卡顺牌
	public boolean isTingCardShun = false;
	
	public int hunUsed=0;//用掉了几个混

	//混是否为小鸡
	public boolean hunisxiaoji = false ;
	
	public int useHuncardAsNormalNum = 0;
	
	//
	public TingCheckResult()
	{
		
	}
	public void clear_all()
	{
		checked.clear();
		unchecked.clear();
		checked=null;
		unchecked=null;
		keziNum=0;
		shunZiNum=0;
	}
	
	public void reset()
	{
		checked.clear();
		unchecked.clear();
		keziNum=0;
		shunZiNum=0;
		danDiao = 0;
		duiZi = 0;
		result = 0;
		hunUsed = 0;
		isTingCardShun = false;
		useHuncardAsNormalNum = 0;
		hunisxiaoji = false;
		jiang2Num = 0;
		jiang5Num = 0;
		jiang8Num = 0;
		has258jiang = false;
	}
	//
	public int getXiaojiShunzi(byte card) {
		for(int i=0;i<checked.size();i++)
		{
			int vvv=checked.get(i);
			int v1 = card;
			int v2 = (card + 1) << 8;
			int v3 = (card + 2) << 16;
			int shunziValue = v1 | v2 | v3;
			if (vvv == shunziValue) {
				return vvv;
			}
		}
		
		return 0;
	}
	
	public int getXiaojiShunziNum(byte card, int cmd) 
	{
		int num = 0;
		int shunziValue1 = 0;
		int shunziValue2 = 0;
		int shunziValue3 = 0;
		if ((card & 0x0f) != 0x08 && (card & 0x0f) != 0x09) {
			shunziValue1 = card | ((card + 1) << 8) | ((card + 2) << 16);
		}
		if ((card & 0x0f) != 0x02 && (card & 0x0f) != 0x01) {
			shunziValue2 = (card-2) | ((card - 1) << 8) | (card << 16);
		}
		if ((card & 0x0f) != 0x01 && (card & 0x0f) != 0x09) {
			shunziValue3 = (card-1) | (card << 8) | ((card+1) << 16);
		}

		for(int i=0;i<checked.size();i++) {
			int value = checked.get(i);
			if (value == shunziValue1 || value == shunziValue2 || value == shunziValue3 )
			{
				num++;
			}
		}
		return num;
	}
	
	public int hasTwoSameShunzi()
	{
		for(int i=0;i<checked.size();i++)
		{
			int vvv=checked.get(i);
			int v1=vvv&0xff;
			int v2=(vvv>>8)&0xff;
			int v3=(vvv>>16)&0xff;
			int t1=v1+v2+v3;
			
			for(int k=i+1;k<checked.size();k++)
			{
				int vv=checked.get(k);
				int vv1=vv&0xff;
				int vv2=(vv>>8)&0xff;
				int vv3=(vv>>16)&0xff;
				 
				int t2=vv1+vv2+vv3;
				if(t1==t2)
					return 1;
			}
		}
		return 0;
	}
	
	public int getKaShunzi(byte card) {
		for(int i=0;i<checked.size();i++)
		{
			int vvv=checked.get(i);
			int v1 = card - 1;
			int v2 = card << 8;
			int v3 = (card + 1) << 16;
			
			int shunzi = v1 | v2 | v3;
			if (vvv == shunzi) {
				return shunzi;
			}
			
		}
		return 0;
	}
	
	public boolean find_dui_zi(int cd) {
		int d1=duiZi&0xffff;
		int d2=(duiZi>>16)&0xffff;
		
		int v1 = cd;
		int v2 = cd << 8;
		int duizi = v1 | v2;
		if (duizi == d1 || duizi == d2) {
			return true;
		}
		
		return false;
	}
	//
	public boolean find_ke_zi(int cd)
	{
		 
		//
		for(int i=0;i<checked.size();i++)
		{
			int vvv=checked.get(i);
			int v1=vvv&0xff;
			int v2=(vvv>>8)&0xff;
			int v3=(vvv>>16)&0xff;
			//
			if(v1==v2 && v2==v3 && cd==v1)
			{
			 
				return true;
			}
		}
		//
		return false;
	}
	
	public int continue3KeZi()
	{

		for(int i=0;i<checked.size();i++)
		{
			int vvv=checked.get(i);
			int v1=vvv&0xff;
			int v2=(vvv>>8)&0xff;
			int v3=(vvv>>16)&0xff;
			//
			if(v1==v2 && v2==v3 )
			{
				 if(find_ke_zi(v1-1) && find_ke_zi(v1-2) )
					 return 1;
				 
				 if(find_ke_zi(v1-1) && find_ke_zi(v1+1) )
					 return 1;
				 
				 if(find_ke_zi(v1+2) && find_ke_zi(v1+1) )
					 return 1;
			}
		}
		
		return 0;
	}
	
	//
	public void removeKe(int cd)
	{
		if(keziNum==0)
			return;
		//
		for(int i=0;i<checked.size();i++)
		{
			int vvv=checked.get(i);
			int v1=vvv&0xff;
			int v2=(vvv>>8)&0xff;
			int v3=(vvv>>16)&0xff;
			if(v1==v2 && v2==v3 && cd==v1)
			{
				keziNum--;
				return;
			}
		}
	}
	//是否可以再加一个对子
	public boolean couldAddMoreDuizi()
	{
		int d1=duiZi&0xffff;
		int d2=(duiZi>>16)&0xffff;
		if(d1>0 && d2>0)
			return false;
		
		if((d1>0 || d2>0) && isTingCardShun)
			return false;
		
		//
	
		//
		return true;
		
	}

	//是否可以再加一个对子
	public boolean couldAddMoreDuizi(int hongzhongNnum) {
		int d1=duiZi&0xffff;
		int d2=(duiZi>>16)&0xffff;
		if(d1>0 && d2>0)
			return false;
		
		if((d1>0 || d2>0) && isTingCardShun)
			return false;
		
		//
		if(hongzhongNnum==2 && d1==00 && d2==0) {
			return true;
		} else {
			//
			return true;
		}
	}
	//可能听牌的结果中，有几个对子
	public int getDuiziNum() {
		int num=0;
		int d1=duiZi&0xffff;
		int d2=(duiZi>>16)&0xffff;
		if(d1>0)
			num++;
		if(d2>0)
			num++;
		//
		return num;
	}
	public int getShunZiNum()
	{
		return shunZiNum;
	}
	//
	public void addDuizi(byte b) {
		int d1=duiZi&0xffff;
		int d2=(duiZi>>16)&0xffff;
		if(d1>0 && d2==0) {
			duiZi=(b<<16)|(b<<24)|d1;
		}
		//
		if(d1==0 && d2==0) {
			duiZi=(b<<8)|b;
		}
	}
	//
 
	//
	public boolean foundCard(byte b) {
		for(int i=0;i<unchecked.size();i++) {
			byte bb=unchecked.get(i);
			if(bb==b)
				return true;
		}
		//
		return false;
	}
	//
	public void remove (byte b) {
		for(int i=0;i<unchecked.size();i++) {
			byte bb=unchecked.get(i);
			if(bb==b) {
				unchecked.remove(i);
				break;
			}
		}

	}
	//
	public boolean hasCards(byte b1,byte b2,byte b3) {
		boolean found_b1=false;
		boolean found_b2=false;
		boolean found_b3=false;
		for(int i=0;i<unchecked.size();i++) {
			byte bb=unchecked.get(i);
			if(bb==b1) {
				found_b1=true;
			}
			if(bb==b2) {
				found_b2=true;
			}
			if(bb==b3) {
				found_b3=true;
			}
		}
		//
		if(found_b1 && found_b2&& found_b3)
			return true;
		//
		return false;
	}
	
	//
	public void copy(TingCheckResult right) {
		this.danDiao=right.danDiao;
		this.duiZi=right.duiZi;
		this.shunZi=right.shunZi;
		this.result=right.result;
		this.keziNum=right.keziNum;
		this.shunZiNum=right.shunZiNum;
		this.isTingCardShun=right.isTingCardShun;
		//this.hastingjia = right.hastingjia;
		 this.hunUsed = right.hunUsed;
		 this.hunisxiaoji = right.hunisxiaoji;
		//
		this.checked.clear();
		for(int i=0;i<right.checked.size();i++) {
			int cc=right.checked.get(i);
			checked.add(cc);
		}
		//
		this.unchecked.clear();
		for(int i=0;i<right.unchecked.size();i++) {
			byte bb=right.unchecked.get(i);
			unchecked.add(bb);
		}
	}
	
	public int getSpecifiedShunziNum(int cd) {
		int shunNum = 0;
		int shunType1 = 0;
		int shunType2 = 0;
		int shunType3 = 0;
		if ((cd&0x0f) == 0x02) {
			shunType1 = (cd - 1) | (cd << 8) | ((cd + 1) << 16);
			shunType2 = cd | ((cd + 1) << 8) | ((cd + 2) << 16);
		} else if ((cd&0x0f) == 0x08) {
			shunType1 = (cd - 2) | ((cd - 1) << 8) | (cd << 16);
			shunType2 = (cd - 1) | (cd << 8) | ((cd + 1) << 16);
		} else if ((cd&0x0f) == 0x05) {
			shunType1 = (cd - 2) | ((cd - 1) << 8) | (cd << 16);
			shunType2 = (cd - 1) | (cd << 8) | ((cd + 1) << 16);
			shunType3 = cd | ((cd + 1 )<< 8) | ((cd + 2) << 16);
		}
		for (int i = 0; i < checked.size(); ++i) {
			int value = checked.get(i);
			if (value == shunType1 || value == shunType2 || value == shunType3) {
				shunNum++;
			}
		}
		
		return shunNum;
	}
}
